package com.zlk.algorithm.algorithm.dynamicPlan.mutiKnapsack75;

import java.io.*;

// 观赏樱花
// 给定一个背包的容量t，一共有n种货物，并且给定每种货物的信息
// 花费(cost)、价值(val)、数量(cnt)
// 如果cnt == 0，代表这种货物可以无限选择
// 如果cnt > 0，那么cnt代表这种货物的数量
// 挑选货物的总容量在不超过t的情况下，返回能得到的最大价值
// 背包容量不超过1000，每一种货物的花费都>0
// 测试链接 : https://www.luogu.com.cn/problem/P1833
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的code，提交时请把类名改成"Main"，可以直接通过
public class Code03_CherryBlossomViewing {

    public static final int MAXT=1000;
    public static final int MAXN=10000;

    public static int[] COST = new int[MAXN+1];
    public static int[] VAL = new int[MAXN+1];
    public static int[] CNT = new int[MAXN+1];
    public static int hour1, minute1, hour2, minute2;
    public static int n,t,m;

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(bufferedReader);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken()!=-1){
            hour1 = (int) in.nval;
            // 跳过冒号
            in.nextToken();
            in.nextToken();
            minute1 = (int) in.nval;
            in.nextToken();
            hour2 = (int) in.nval;
            // 跳过冒号
            in.nextToken();
            in.nextToken();
            minute2 = (int) in.nval;
            if (minute1 > minute2) {
                hour2--;
                minute2 += 60;
            }
            // 计算背包容量
            t = (hour2 - hour1) * 60 + minute2 - minute1;
            in.nextToken();
            n = (int) in.nval;
            m = 0;
            for (int i = 1,cost,val,cnt; i <=n; i++) {
                in.nextToken();cost=(int) in.nval;
                in.nextToken();val=(int) in.nval;
                in.nextToken();cnt=(int) in.nval;
                if(cnt==0){
                    cnt = MAXT;
                }
                for (int k = 1; k<=cnt ; k<<=1) {
                    COST[++m]=k*cost;
                    VAL[m] = k*val;
                    cnt-=k;
                }
                if(cnt>0){
                    COST[++m] = cnt*cost;
                    VAL[m] = cnt*val;
                }
            }
            out.println(compute1());
        }

    }

    private static int compute1() {
        int dp[] = new int[t+1];
        for (int i = 1; i <=m ; i++) {
            for (int j =t; j >=COST[i] ; j--) {
                dp[j]=Math.max(dp[j],dp[j-COST[i]]+VAL[i]);
            }
        }
        return dp[t];
    }

}
